import time
from pathlib import Path

import numpy as np
import pandas as pd

from StressAna.Lib.Model import Globalvariables
from StressAna.Lib.Utils import DateTimeUtil


class MonAna():
    def __init__(self, file):
        super().__init__()
        self.srcFile = file
        self._date = ''
        self._name = ''
        self._data = ''
        self.raw_data = []
        self._df = pd.DataFrame(columns=Globalvariables.COLUMNS)
        self._init()

    @property
    def data(self):
        if not self._data:
            self._data = np.zeros([3, len(self.raw_data[0])], dtype='float')
            dates, temps, powers = self.raw_data[0], self.raw_data[1], self.raw_data[2]
            stamps = [DateTimeUtil.formatTimeStamp(DateTimeUtil.parseDateTimeBy(x, "%Y-%m-%d %H:%M:%S")) for x in dates]
            self._data[0] = stamps
            self._data[1] = [float(x) for x in temps]
            self._data[2] = [float(x) for x in powers]
        return self._data

    @property
    def date(self):
        return self._date

    @property
    def name(self):
        return self._name

    @property
    def df(self):
        if self._df.empty:
            dates, temps, powers = self.raw_data[0], self.raw_data[1], self.raw_data[2]
            self._df = pd.DataFrame(
                data={'sID': self.name, 'time': dates, 'temp': temps, 'power': powers})
            self._df['time'] = self._df['time'].astype('datetime64[ns]')
            self._df['temp'] = self._df['temp'].astype('float')
            self._df['power'] = self._df['power'].astype('float')
        return self._df

    def _init(self):
        with open(self.srcFile, 'r') as f:
            lines = f.read().splitlines()
        self._name = Path(self.srcFile).name[:3]
        self._date = Path(self.srcFile).name[4:14]
        lines_list = [x.split(',') for x in lines]
        self.raw_data = np.array(lines_list).transpose(1, 0)


if __name__ == '__main__':
    a = MonAna(r'D:\LK\TMV煤矿项目\应力数据分析\monidata\1\G01/G01_2024-01-24.txt')
    print(a.df)
    print(a.data)
